package com.example.managemyself.data.model;

import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.ForeignKey;
import androidx.room.Index;

import java.util.Date;

/**
 * 日记附件实体类
 * 用于存储日记的多媒体附件信息
 */
@Entity(tableName = "diary_attachments",
        foreignKeys = @ForeignKey(
                entity = DiaryEntry.class,
                parentColumns = "id",
                childColumns = "diaryId",
                onDelete = ForeignKey.CASCADE
        ),
        indices = {@Index("diaryId")})
public class DiaryAttachment {
    
    @PrimaryKey(autoGenerate = true)
    private int id;
    
    private int diaryId; // 关联的日记ID
    private String type; // 附件类型：audio, photo, video
    private String fileName; // 文件名
    private String filePath; // 文件路径
    private long fileSize; // 文件大小（字节）
    private Date createdDate; // 创建时间
    private String description; // 附件描述
    
    public DiaryAttachment() {
        this.createdDate = new Date();
    }
    
    public DiaryAttachment(int diaryId, String type, String fileName, String filePath) {
        this.diaryId = diaryId;
        this.type = type;
        this.fileName = fileName;
        this.filePath = filePath;
        this.createdDate = new Date();
    }
    
    // Getters and Setters
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public int getDiaryId() {
        return diaryId;
    }
    
    public void setDiaryId(int diaryId) {
        this.diaryId = diaryId;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getFileName() {
        return fileName;
    }
    
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public String getFilePath() {
        return filePath;
    }
    
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    
    public long getFileSize() {
        return fileSize;
    }
    
    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }
    
    public Date getCreatedDate() {
        return createdDate;
    }
    
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * 获取文件大小的可读格式
     */
    public String getFormattedFileSize() {
        if (fileSize < 1024) {
            return fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            return String.format("%.1f KB", fileSize / 1024.0);
        } else {
            return String.format("%.1f MB", fileSize / (1024.0 * 1024.0));
        }
    }
    
    /**
     * 检查是否为音频文件
     */
    public boolean isAudio() {
        return "audio".equals(type);
    }
    
    /**
     * 检查是否为图片文件
     */
    public boolean isPhoto() {
        return "photo".equals(type);
    }
    
    /**
     * 检查是否为视频文件
     */
    public boolean isVideo() {
        return "video".equals(type);
    }
} 